<template>
  <el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="70px">
  <el-form-item label="分类名称" prop="cate_name">
    <el-input v-model="ruleForm.cate_name"></el-input>
  </el-form-item>
  <el-form-item label="分类别名" prop="cate_alias">
    <el-input v-model="ruleForm.cate_alias"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="addList">确认添加</el-button>
    <!-- 重置待修复，先凑合用 -->
    <el-button type="info" @click="reset()">重置</el-button>
  </el-form-item>
  </el-form>
</el-dialog>
</template>

<script>
export default {
  name: 'VueEv2Addlist',
  props: {
    dialogVisible: {
      typeof: Boolean,
      default: false
    },
    ruleForm: {
      typeof: Object,
      required: true
    }
  },

  data () {
    return {
      rules: {
        cate_alias: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称必须是1-10位的非空字符串', trigger: 'blur' }
        ],
        cate_name: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[0-9a-zA-Z]{1,15}$/, message: '分类别名必须是1-15位的字母和数字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleClose () {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.$emit('dialogVisibleBtn', false)
          this.reset()
        })
        .catch(_ => {})
    },
    addList () {
      this.$refs.ruleForm.validate(valid => {
        if (!valid) return
        if (this.ruleForm.id) {
          this.$emit('updateList', this.ruleForm)
        } else {
          this.$emit('addList', this.ruleForm)
        }
        this.reset()
      })
    },
    reset () {
      for (const key in this.ruleForm) {
        this.ruleForm[key] = ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
